package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.persistence.DBUtil;
import csu.web.mypetstore.persistence.LogDao;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class LogDaoImpl implements LogDao {

    private final String INSERTLOG_TO_LOG = "INSERT INTO logs (timestamp, message) VALUES ( ?, ?)";

    @Override
    public void insertLog(String message){
        try(Connection connection = DBUtil.getConnection()) {
            PreparedStatement insertStatement = connection.prepareStatement(INSERTLOG_TO_LOG);
            LocalDateTime now = LocalDateTime.now();
            Timestamp timestamp = Timestamp.valueOf(now);
            insertStatement.setTimestamp(1, timestamp);
            insertStatement.setString(2,message);
            insertStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
